<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends CommonController {



    public function index(){
        $sql="select * from user";
        $res=M('user')->query($sql);
        $this->assign('user',$res);
        $this->display();
    }
    //  会员停用
    public function userstop()
    {
    	$uid=$_POST['uid'];
    	$sql="update user set ustate='1' where id='{$uid}'";
    	if (M('user')->execute($sql)) {
    		$arr=array(
    			'error'=>0,
    			'info'=>'己停用'
    			);
    		echo json_encode($arr);
    		exit();
    	 }else{
    	 	$arr=array(
    			'error'=>1,
    			'info'=>'停用失败'
    			);
    		echo json_encode($arr);
    		exit();
    	 } ;
    }
    //  会员启用
    public function userstart()
    {
        $uid=$_POST['uid'];
        $sql="update user set ustate='0' where id='{$uid}'";
        if (M('user')->execute($sql)) {
            $arr=array(
                'error'=>0,
                'info'=>'己启用'
                );
            echo json_encode($arr);
            exit();
         }else{
            $arr=array(
                'error'=>1,
                'info'=>'启用失败'
                );
            echo json_encode($arr);
            exit();
         } ;
    }
    //  会员删除
    public function userdel()
    {
        $uid=$_POST['uid'];
        $sql="update user set ustate='2' where id='{$uid}'";
        if (M('user')->execute($sql)) {
            $arr=array(
                'error'=>0,
                'info'=>'己删除'
                );
            echo json_encode($arr);
            exit();
         }else{
            $arr=array(
                'error'=>1,
                'info'=>'删除失败'
                );
            echo json_encode($arr);
            exit();
         } ;
    }
    //批量删除
    public function delall()
    {
        //接到字符串 去最后一位 ',' 
        $num=rtrim($_POST['num'],',');

        $sql="update user set ustate='2' where id in($num)";

        //执行判断
        if(M('user')->execute($sql)){
            $arr=array(
                'error'=>0,
                'info'=>'删除成功'
                );
            echo json_encode($arr);
            exit();
        }else{
            $arr=array(
                'error'=>1,
                'info'=>'删除失败'
                );
            echo json_encode($arr);
            exit();
        };

    }

    //  修改信息页---输出内容在表单
    public function userinfoedit()
    {
        $uid=$_GET['id'];
        $sql="select * from user where id='{$uid}'";
        $res=current(M('user')->query($sql));
        $this->assign('user',$res);
        $this->display();


    }
    //  ajax 修改信息
    public function EditInfo()
    {   
        $id=$_POST['uid'];
        //检测邮箱账号
        $email=$_POST['email'];
        $sql="select email from user where id<>'{$id}' and email='{$email}'";
        $res=M()->query($sql);
        if ($res) {
            $arr = array(
                'error'=>1,
                'info'=>'邮箱己存在,请重新输入！'
                );
            echo json_encode($arr);
            exit;
        }
        //检测昵称
        $nickname=$_POST['nickname'];
        $sql="select nickname from user where id<>'{$id}' and nickname='{$nickname}'";
        $res=M()->query($sql);
        if ($res) {
            
            $arr = array(
                'error'=>1,
                'info'=>'昵称己存在，请重新输入！'
                );
            echo json_encode($arr);
            exit;
        }

        //获取POST信息
        $data=$_POST;
        //账号全部转小写
        $data['email'] =strtolower($data['email']);
        //邮箱激活状态
        $data['estate']=0;
        //更新数据信息
        if (M('user')->where("id='$id'")->save($data)) {
            $arr = array(
                'error'=>0,
                'info'=>'修改成功！'
                );
            echo json_encode($arr);
            exit;
        }else{
            $arr = array(
                'error'=>0,
                'info'=>'修改成功！'
                );
            echo json_encode($arr);
            exit;
        }

    }
    //修改密码页
    public function userpasswordedit()
    {
        $id=$_GET['id'];
        $sql = "select id,nickname from user where id='{$id}'";
        $res=current(M('user')->query($sql));
        $this->assign('info',$res);
        $this->display();
    }
    //ajax 修改密码
    public function EditPassword()
    {
        $id=$_GET['id'];
        //POST 获取两次密码
        $password=$_POST['password'];
        $repassword=$_POST['repassword'];
        $passlen=strlen($password); //获取密码长度
        //判断密码长度
        if ($passlen>=6 and $passlen<=16) {
            //判断两次密码是否相同
            if ($password!=$repassword) {
                $arr = array(
                    'error'=>2,
                    'info'=>'两次密码不一致'
                    );
                echo json_encode($arr);
                exit;
            }
        }else{
            $arr = array(
                    'error'=>1,
                    'info'=>'密码要求6到16个字符'
                    );
                echo json_encode($arr);
                exit;
        }
        

        //POST获取表单密码
        $data['password']=md5($password);
        //更新密码
        if (M('user')->where("id='$id'")->save($data)) {
            $arr = array(
                'error'=>0,
                'info'=>'修改成功！'
                );
            echo json_encode($arr);
            exit;
        }else{
            $arr = array(
                'error'=>3,
                'info'=>'修改失败,新密码与原密码相同！'
                );
            echo json_encode($arr);
            exit;
        }

    }
    //增加会员
    public function useradd()
    {
        
    }


}